package com.shuijing.boot.transaction.controller;


import com.shuijing.boot.transaction.service.TransactionService;
import com.shuijing.boot.transaction.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/transaction")
@Api(value = "User对象", tags = "事务测试")
public class TransactionController {

    @Autowired
    private TransactionService service;

    @ApiOperation(value = "清空所有数据")
    @GetMapping(value = "/clean")
    public String clean() {
        try {
            service.clean();
            return "清空所有数据成功";
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @ApiOperation(value = "不使用事务")
    @GetMapping(value = "/noTransaction/{id}")
    public String noTransaction(@PathVariable Integer id) {
        try {
            switch (id) {
                case 1:
                    service.noTransaction_required_required_externalException();
                    break;
                case 2:
                    service.noTransaction_required_requiredException();
                    break;
            }
            return "正常执行";
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @ApiOperation(value = "Required事务")
    @GetMapping(value = "/required/{id}")
    public String required(@PathVariable Integer id) {
        try {
            switch (id) {
                case 1:
                    service.transaction_required_required_externalException();
                    break;
                case 2:
                    service.transaction_required_requiredException();
                    break;
                case 3:
                    service.transaction_required_requiredException_try();
                    break;
                case 4:
                    service.transaction_required_requiredNew_externalException();
                    break;
                case 5:
                    service.transaction_required_requiredNew_requiredNewException();
                    break;
                case 6:
                    service.transaction_required_requiredNewException_try();
                    break;
            }
            return "正常执行";
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @ApiOperation(value = "RequiredNew事务")
    @GetMapping(value = "/requiredNew/{id}")
    public String requiredNew(@PathVariable Integer id) {
        try {
            switch (id) {
                case 1:
                    service.transaction_required_requiredNew_externalException();
                    break;
                case 2:
                    service.transaction_required_requiredNew_requiredNewException();
                    break;
                case 3:
                    service.transaction_required_requiredNewException_try();
                    break;
            }
            return "正常执行";
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @ApiOperation(value = "Nested事务")
    @GetMapping(value = "/nested/{id}")
    public String nested(@PathVariable Integer id) {
        try {
            switch (id) {
                case 1:
                    service.transaction_nested_nested_externalException();
                    break;
                case 2:
                    service.transaction_nested_nestedException();
                    break;
                case 3:
                    service.transaction_nested_nestedException_try();
                    break;
            }
            return "正常执行";
        } catch (Exception e) {
            return e.getMessage();
        }
    }
}
